package com.example.neusoft.sqlitedemo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditContactActivity extends AppCompatActivity implements View.OnClickListener {
    Button button_refresh, button_delete,button_return;
    EditText edit_name, edit_tel, edit_type;
    Context context;
    ContactDBAdapter dbAdapter;
    int _id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_contact);
        context = EditContactActivity.this;
        initView();
        dbAdapter=ContactDBAdapter.getInstance(context);
        //获取传进的参数，并显示到相应控件中
        Intent intent=getIntent();
        _id=intent.getIntExtra("_id", 0);
        edit_name.setText(intent.getStringExtra("name"));
        edit_tel.setText(intent.getStringExtra("tel"));
        edit_type.setText(intent.getStringExtra("type"));
    }

    void initView() {
        edit_name = (EditText) findViewById(R.id.edittext_addcontact_name);
        edit_tel = (EditText) findViewById(R.id.edittext_addcontact_tel);
        edit_type = (EditText) findViewById(R.id.edittext_addcontact_type);
        button_refresh = (Button) findViewById(R.id.button_editcontact_refrersh);
        button_delete = (Button) findViewById(R.id.button_editcontact_delete);
        button_return = (Button) findViewById(R.id.button_editcontact_return);
        button_refresh.setOnClickListener(this);
        button_delete.setOnClickListener(this);
        button_return.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.button_editcontact_refrersh:
                String name = edit_name.getText().toString();
                String tel = edit_tel.getText().toString();
                String type = edit_type.getText().toString();

                if (TextUtils.isEmpty(name)) {
                    edit_name.setError("请输入名字");
                    return;
                }
                if (TextUtils.isEmpty(tel)) {
                    edit_tel.setError("请输入电话");
                    return;
                }
                if (TextUtils.isEmpty(type)) {
                    edit_type.setError("请输入类型");
                    return;
                }

                ContactBean contactBean = new ContactBean();
                contactBean.set_id(_id);
                contactBean.setName(name);
                contactBean.setTel(tel);
                contactBean.setType(type);
                dbAdapter.update(contactBean);
                finish();
                break;

            case R.id.button_editcontact_delete:
                dbAdapter.deleteById(_id);
                finish();
                break;

            case R.id.button_editcontact_return:
                finish();
                break;
        }
    }

}

